package cn.new7as.cas5s.ui.repay;


import static cn.new7as.cas5s.tool.util.Utils.CAMERA_A;
import static cn.new7as.cas5s.tool.util.Utils.getList;
import static cn.new7as.cas5s.ui.apply.Info_ThirdActivity.ALBUM_A;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.http.listener.OnUpdateListener;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.databinding.ActivitySubproofBinding;
import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.OrderApi;
import cn.new7as.cas5s.ui.easy.netapi.UpdateImageApi;
import cn.new7as.cas5s.ui.easy.netapi.UtrSendApi;


public class SubProofActivity extends BaseActivity {
    private String[] rlphotp = new String[]{"Camera", "Album"};
    protected String[] needPermissions = {
            Manifest.permission.CAMERA,
    };
    private ActivitySubproofBinding binding;
    private List<UtrSendApi.Data> mDatas = new ArrayList<>();
    private Utradapter utradapter;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySubproofBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setop("Submit Payment Proof");
        initV();
        UtrSendApi.Data dataBean = new UtrSendApi.Data();
        mDatas.add(dataBean);
        utradapter = new Utradapter(mDatas, this);
        binding.monthGrid.setAdapter(utradapter);

    }

    @Override
    public void initV() {
        super.initV();
        binding.verifyme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendutr();
            }
        });

    }

    private void sendutr() {
        String aa = binding.amount.getText().toString();
        String ab = binding.accountnumberBa.getText().toString();
        String ba = binding.upi.getText().toString();
        String data = getIntent().getStringExtra("data");

        show();
        EasyHttp.post(this)
                .api(new UtrSendApi(getunique(), aa, ab, ba,data, getList(mDatas))).request(new OnHttpListener<HttpData<OrderApi.OrderData>>() {

                    @Override
                    public void onHttpSuccess(@NonNull HttpData<OrderApi.OrderData> result) {
                        hide();
                        finish();
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });
    }


    void upload(File file) {
        show();
        EasyHttp.post(this)
                .api(new UpdateImageApi(file))
                .request(new OnUpdateListener<HttpData<String>>() {
                    @Override
                    public void onUpdateProgressChange(int progress) {

                    }

                    @Override
                    public void onUpdateSuccess(HttpData<String> result) {
                        hide();

                        UtrSendApi.Data dataBean = new UtrSendApi.Data();
                        dataBean.hjadkvsa = result.getResult();


                      /*  for (int i = 0; i < mDatas.size(); i++) {
                            if (mDatas.get(i).jlkljoiycxdd) {
                                index = i;
                            }
                        }*/
                        if (mDatas.size() < 9) {

                            mDatas.add( mDatas.size()-1,dataBean);


                            utradapter.notifyDataSetChanged();

                        }

                    }

                    @Override
                    public void onUpdateFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }

                });

    }

    void showPhoto() {
        if (XXPermissions.isGranted(this, needPermissions)) {
            Utils.getcameimg(SubProofActivity.this, 0);

        } else
            XXPermissions.with(SubProofActivity.this)

                    .permission(needPermissions)

                    .request(new OnPermissionCallback() {

                        @Override
                        public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                            Utils.getcameimg(SubProofActivity.this, 0);

                        }

                        @Override
                        public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {

                        }
                    });


    }


    void showPhoto2() {
        new XPopup.Builder(this)
                .isDestroyOnDismiss(true)

                .asBottomList("", rlphotp,
                        null, -1,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                if (position == 0) {
                                    showPhoto();
                                } else {
                                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                                    intent.setType("image/*");
                                    startActivityForResult(intent, ALBUM_A);
                                }

                            }
                        })
                .show();
    }


    @SuppressLint("MissingSuperCall")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {
            Bitmap bitmap;

            switch (requestCode) {
                case CAMERA_A:

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        bitmap = Utils.getBitmapFromUri(SubProofActivity.this, Utils.getUri(SubProofActivity.this));
                    } else {
                        bitmap = Utils.getBitmapFromUri(SubProofActivity.this, Uri.fromFile(Utils.imgFile));
                    }


                    upload(Utils.saveBitmapFile(getBaseContext(), bitmap));
                    break;
                case ALBUM_A:
                    bitmap = Utils.getBitmapFromUri(SubProofActivity.this, data.getData());

                    upload(Utils.saveBitmapFile(getBaseContext(), bitmap));
                    break;
                default:
                    break;
            }
        }


    }
}